<template>
  <a-layout>
    <a-layout-content style="height: calc(100vh - 88px); padding: 2px">
      <VxeBasicTable ref="TableRef" v-bind="options">
        <template #toolbar_buttons>
          <a-space :size="2" style="padding-left: 5px">
            <a-button
              v-permission="'sysNotice:add'"
              type="primary"
              @click="handleEdit()"
            >
              新增
            </a-button>
            <a-button type="primary" @click="handleOpenFilter"> 过滤 </a-button>
          </a-space>
        </template>
        <template #action="{ row }">
          <a-space :size="2">
            <a-button
              type="dashed"
              v-permission="'sysNotice:public'"
              :disabled="row.status === 1"
              @click="publicNotice(row)"
            >
              发布
            </a-button>
            <a-button
              type="dashed"
              v-permission="'sysNotice:update'"
              :disabled="row.status === 1"
              @click="handleEdit(row)"
            >
              编辑
            </a-button>
            <a-button
              type="dashed"
              v-permission="'sysNotice:delete'"
              danger
              :disabled="row.status === 1"
              @click="handleDelete(row)"
            >
              删除
            </a-button>
          </a-space>
        </template>
      </VxeBasicTable>
    </a-layout-content>
    <NoticeEdit v-model:visible="showEdit" :data="current" @done="reload" />
    <TableFilter
      ref="FilterRef"
      @done="reload"
      v-model:visible="ifSHowFilter"
      :filter-cols="Columns"
    />
  </a-layout>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue/es';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { messageLoading } from 'ele-admin-pro/es';
  import NoticeEdit from './components/notice-edit.vue';
  import { page, publicPost, remove } from '@/api/system/notice';
  import type { SysConfig } from '@/api/system/config/model';
  import { useConfig } from './config';

  const { TableRef, FilterRef, Columns, TableFilter, options } =
    useConfig(page);

  // 当前选中数据
  const current = ref<SysConfig>();

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  const ifSHowFilter = ref(false);

  /* 刷新表格 */
  const reload = () => {
    TableRef.value?.commitProxy('query');
  };

  /* 编辑 */
  const handleEdit = (row?: SysConfig) => {
    current.value = row;
    showEdit.value = true;
  };
  const handleOpenFilter = () => {
    ifSHowFilter.value = true;
  };
  // 发布
  const publicNotice = (row: any) => {
    Modal.confirm({
      title: '提示',
      content: `确定发布通知公告：【${row.title}】，不可撤销?`,
      centered: true,
      icon: createVNode(ExclamationCircleOutlined),
      async onOk() {
        await publicPost(row.id);
        TableRef.value?.commitProxy('query');
        message.success('发布成功');
      }
    });
  };
  /* 删除 */
  const handleDelete = (row: SysConfig) => {
    const hide = messageLoading('请求中..', 0);
    remove(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
</script>

<script lang="ts">
  export default {
    name: 'SystemNotice'
  };
</script>
